package com.example.demo.javaTest;

import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateAndTime {

    @Test
    public void testTime() throws ParseException {

        //jdk<8，不推荐
        Date d = new Date();
        System.out.println(d);
        long time = d.getTime();
        System.out.println(time);
        time += 2000;
        Date d2 = new Date(time);
        System.out.println(d2);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
        String rs = sdf.format(d);
        String rs2 = sdf.format(time);
        System.out.println(rs);
        System.out.println(rs2);

        String dateStr = "2024年08月15日 15:18:40 周四 下午";
        Date d3 = sdf.parse(dateStr);
        System.out.println(d3);

        Calendar now = Calendar.getInstance();
        System.out.println(now.get(Calendar.YEAR));
        System.out.println(now.get(Calendar.DAY_OF_YEAR));
        System.out.println(now.getTime());
        System.out.println(now.getTimeInMillis());
        now.set(Calendar.MONTH, 9);//10月
        now.add(Calendar.DAY_OF_YEAR, 10);//加10天
        System.out.println(now);

        //jdk>=8，推荐
        LocalDate ld = LocalDate.now();
        System.out.println(ld);
        System.out.println(ld.getYear());
        System.out.println(ld.getDayOfYear());
        LocalDate ld2 = ld.withYear(2099);
        System.out.println(ld2);
        System.out.println(ld);
        LocalDate ld3 = ld.plusYears(2);
        System.out.println(ld3);
        LocalDate ld4 = LocalDate.of(2099, 12, 12);
        System.out.println(ld4);
        System.out.println(ld4.isAfter(ld));

        //api同上
        LocalTime lt = LocalTime.now();
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(lt);
        System.out.println(ldt);
        LocalDateTime ldt2 = LocalDateTime.of(ld, lt);
        System.out.println(ldt2);

        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);
        System.out.println(ZoneId.getAvailableZoneIds());
        ZoneId zoneId1 = ZoneId.of("America/New_York");
        ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId1);
        System.out.println(zonedDateTime);
        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(Clock.systemUTC());
        System.out.println(zonedDateTime1);
        ZonedDateTime zonedDateTime2 = ZonedDateTime.now();
        System.out.println(zonedDateTime2);

        Instant instant = Instant.now();
        System.out.println(instant.getEpochSecond());
        System.out.println(instant.getNano());

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(formatter.format(ldt));//正向格式化
        System.out.println(ldt.format(formatter));//反向格式化

        LocalDate start = LocalDate.of(2029, 8, 10);
        LocalDate end = LocalDate.of(2029, 8, 15);
        Period period = Period.between(start, end);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        LocalDateTime start1 = LocalDateTime.of(2029, 8, 10, 11, 11, 11);
        LocalDateTime end1 = LocalDateTime.of(2029, 8, 10, 11, 11, 12);
        Duration duration = Duration.between(start1, end1);
        System.out.println(duration.toDays());
        System.out.println(duration.toNanos());

    }

}
